package com.springmvc.shangguigu.converters;

import com.springmvc.shangguigu.entities.Department;
import com.springmvc.shangguigu.entities.Employee;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * EmployeeConverter--SpringMVC-test
 *
 * @author: Jovi
 * @createTime: 2018-09-12 11:33
 **/
@Component
public class EmployeeConverter implements Converter<String, Employee> {

    @Override
    public Employee convert(String source) {
        String[] vals = source.split("-");
        if (vals != null && vals.length == 4) {
            String lastName = vals[0];
            String email = vals[1];
            Integer gender = Integer.parseInt(vals[2]);
            Department department = new Department();
            department.setId(Integer.parseInt(vals[3]));

            Employee employee = new Employee(null, lastName, email, gender, department);

            System.out.println(source + "--convert--" + employee);
            return employee;
        }
        return null;
    }
}
